package com.wbp.cms.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wbp.cms.client.constant.CommonConstant;
import com.wbp.cms.client.result.Result;
import com.wbp.cms.client.util.JsonUtils;
import com.wbp.cms.core.service.image.ImageUploadYunManager;
import com.wbp.cms.core.util.CommonProperties;


/**
 * @Description:
 * @Department:
 * @author andy.lhc
 * @date 2016年3月14日 下午12:32:33
 */
public class BaseController {

  protected  static final Integer TYPE_SELECT_PRODUCT  = 10 ; // 从预约项目中选择产品  
  
  @Resource(name="imageUploadYunManager")
  private ImageUploadYunManager  imageUploadYunManager ;
  
  protected static List<String> superUsers = Lists.newArrayList() ;
  private static String  super_Users = CommonProperties.getInstance().getProperties(CommonConstant.superUsers) ;
  static{
      if(super_Users !=null && !super_Users.equals("")){
          String[] slist = super_Users.split(",") ;
          for(String s:slist)
            superUsers.add(s) ;
      }
  }
  
  /**
   * 产品页面的default image 
   */
  protected static final String product_default_image_url = CommonProperties.getInstance().getProperties("product_default_image_url") ;
  
   /**
    * dataTables 请求时所用的key
    */
   protected  static  final  String DT_KEY_DRAW ="draw" ;
   /**
    * dataTables，检索时所用的key
    */
   protected static final  String  DT_KEY_SEARCH_VALUE ="search[value]" ;
   

  public Map<String, String> getParameters(HttpServletRequest request) {
    Map<String, String> map = Maps.newTreeMap();
 
    Map<String,String[]> map1 = request.getParameterMap();
    for (Object key : map1.keySet()) {
         // System.out.println(key+":"+request.getParameter(key.toString()));
        map.put(key.toString(), request.getParameter(key.toString())) ;
    }

    return map;
  }

  /**
   * 得到请求参数
   * @param request
   * @param claz
   * @return
   */
  protected <T> T getRequestObject(HttpServletRequest request,Class<T> claz ){
    String params = this.getParameters(request).get("params") ;   
    if(params == null  || params.equals(""))
        throw new IllegalArgumentException("request中没有params参数,请检查请求参数") ;
    T t = JsonUtils.toObject(params, claz) ;
    return t ;
  }

  /**
   * jq  dataTable 封装response 参数
   * @param result
   * @param draw
   * @return
   */
  public <T>  Map<String,Object>  getDataTableMapData(Result<List<T>>  result,String draw){
    Map<String, Object> info = Maps.newHashMap() ;
    info.put("data", ( result.getData()==null ? Lists.newArrayList():result.getData() ) );
    info.put("recordsTotal", result.getTotal());
    info.put("recordsFiltered", result.getTotal());
    info.put("draw", (draw == null || draw.equals("")?"0":draw )  ) ;
    return  info ;
  }

  protected  boolean  isSuperUser(String  userName){
     if(superUsers.contains(userName)) 
       return true ;
     
     return false ;
  }
  
  /**
   * 电话号的中间四位加*
   * @param tmpMobileNumber
   * @return
   */
  protected  String  getMobileNumber(String tmpMobileNumber){
    StringBuilder sb = new StringBuilder();
    if(tmpMobileNumber !=null && !tmpMobileNumber.equals("") && tmpMobileNumber.length() == 11){
      sb.append(tmpMobileNumber.substring(0, 3)) ;
      sb.append("****") ;
      sb.append(tmpMobileNumber.substring(7)) ;
     }
    return sb.toString() ;
 
  }
  /**
   * 身份证号码前14位加*
   * @param tmpIdCard
   * @return
   */
  protected String getIdCard(String tmpIdCard){
    StringBuilder sb = new StringBuilder();
    if(tmpIdCard!=null && !tmpIdCard.equals("") && tmpIdCard.length()>4){
      for(int i=0;i<tmpIdCard.length()-4;i++)
        sb.append("*") ;
      sb.append(tmpIdCard.substring(tmpIdCard.length()-4, tmpIdCard.length()) ) ;
      
     }
    return sb.toString() ;
  }
  
  protected void setExportContentType(HttpServletResponse response){
       setExportContentType(response,"dataReportExport") ;
  }
  
  /**
   * 设置下载的http head
   * @param response
   * @param fileName
   */
  protected void setExportContentType(HttpServletResponse response,String fileName)
  {
       response.setContentType("application/vnd.ms-excel");  // 下载excel格式的contentType
       response.setHeader("Pragma", "No-cache");
       response.setHeader("Cache-Control", "no-cache");
     //  String fileName = "dataReportExport" ;
       response.setHeader("content-disposition", "attachment;filename=" + fileName + ".xls");
  }
  
}
 